﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ClassLibrary.DAO;
using System.Data.SqlClient;
using ClassLibrary.Data;
using System.Collections.Generic;
using ClassLibrary.Entity;


public partial class UserControl_ucServices : System.Web.UI.UserControl
{
    TblEmailDAO EmailDAO = new TblEmailDAO(Global.CONNECTION_STRING);
    public static int index;
    private static string ASCENDING = " ASC";
    private static string DESCENDING = " DESC";
    private static string sortExpression = null;
    private static string direction = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateAlphaPagings();
            this.ViewState["SelectedText"] = "All";
            LoadListEmail();
        }
    }
    #region Sorting......
    private void LoadListEmail()
    {
        BindGrid(ViewState["SelectedText"].ToString());
    }

    private void BindGrid(string StartAlpha)
    {
        DataTable dt_dept = EmailDAO.GetListEmail(StartAlpha);
        DataView dv = new DataView(dt_dept); // dt_dept là table ch?a d? li?u c?a gridview
        dv.Sort = sortExpression + direction;
        grdEmail.DataSource = dv;
        grdEmail.DataBind();
    }
    private SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    protected void grdEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEmail.PageIndex = e.NewPageIndex;
        //LoadDriver();
        BindGrid(ViewState["SelectedText"].ToString());
    }
    private void SortGridView(string sortExpression, string direction, string alphabet)
    {
        DataTable dt_dept = EmailDAO.GetListEmail(alphabet);
        DataView dv = new DataView(dt_dept); // dt_dept là table ch?a d? li?u c?a gridview
        dv.Sort = sortExpression + direction;
        grdEmail.DataSource = dv;
        grdEmail.DataBind();
    }

   protected void grdEmail_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        ViewState["SortExpression"] = sortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING, ViewState["SelectedText"].ToString());
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING, ViewState["SelectedText"].ToString());
        }
    }
     #endregion

    #region Order
       protected void btnMua_Click1(object sender, EventArgs e)
       {
           bool flag = true;    
               try
               {
                   //string IdItem = ((ImageButton)sender).CommandArgument;
                   
                   TblEmail chip = new TblEmail();
                   chip.IDEmail = Convert.ToInt32(((Button)sender).CommandArgument);

                   int IdItem = chip.IDEmail;


                   BasePage.SetShoppingCartSesstion(IdItem + "|0");

                   List<TblBookingDetail> arrItems = new List<TblBookingDetail>();
                   if (Session[Global.SESSION_CART] != null)
                       arrItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];

                   int iNoOfItems = 0;
                   double dTotalAmount = 0;

                   foreach (TblBookingDetail order in arrItems)
                   {
                       iNoOfItems += order.Quantity;
                       dTotalAmount += Convert.ToDouble(order.Quantity) * Convert.ToDouble(order.Price);
                   }

                   flag = true;
                   Session[Global.SESSION_TOTAL] = dTotalAmount;
                   Session[Global.SESSION_CART] = arrItems;


               }
               catch (Exception ex)
               {
                   Session[Global.SESSION_ERROR] = ex;
                   Response.Redirect(Global.ERROR_PAGE, true);
               }
               if (flag)
                   Response.Redirect("~/Services.aspx");
           
       }
          
    #endregion

   #region Alphabet (tim kiem theo ten)

   protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (this.ViewState["SelectedText"] != null)
            {
                LinkButton lbkbtnPaging = (LinkButton)e.Item.FindControl("lnkbtnPaging");
                if (this.ViewState["SelectedText"].ToString() == lbkbtnPaging.Text)
                    lbkbtnPaging.Enabled = false;
            }
        }
    }


    private void CreateAlphaPagings()
    {
        DataTable dt = new DataTable();

        if (this.ViewState["Paging"] == null)
        {
            SqlDataAdapter da = new SqlDataAdapter("sp_Proc_Paging", Global.CONNECTION_STRING);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("@OptionId", SqlDbType.Int).Value = 1;
            da.Fill(dt);
            this.ViewState["Paging"] = dt;
        }
        else
            dt = (DataTable)this.ViewState["Paging"];


        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton lbkbtnPaging = (LinkButton)e.CommandSource;
        BindGrid(e.CommandArgument.ToString());
        this.ViewState["SelectedText"] = e.CommandArgument.ToString();
        CreateAlphaPagings();
    }
    #endregion
    

}
